<?php
namespace app\api\command;

use app\api\controller\Base;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Expire extends Command
{
    protected function configure()
    {
        $this->setName('expire')->setDescription('expire order handle 过期订单处理');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }

    /**
     * 过期订单订单处理  半小时执行一次
     */
    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo '任务正在执行中<br />';return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }

        $map['status'] = 1;
        $map['update_time'] = ['< time',time()-86400*7];
        Db::name('order')->where($map)->chunk(1000,function ($items){
            foreach ($items as $k => $v){
                Db::startTrans();
                try{

                    $data = Base::refund($v,12);
                    if ($data !== true){
                        var_dump($data);
                    }else{
                        Db::commit();
                    }
                }catch (\Exception $e){
                    var_dump($e->getMessage());
                    Db::rollback();
                }
            }
        });

        cache('command'.__CLASS__,null);

    }
}